/**Created by the LayaAirIDE*/
package view
{
	import ui.GameBgUI
	import laya.events.Event;
	import laya.utils.Tween;
	import laya.utils.Handler;
	import laya.ui.Image;

	public class GameBg extends GameBgUI
	{
		private var tw0:Tween;
		private var tw1:Tween;
		public function GameBg() 
		{
			this.on(Event.ADDED,this,onAddToStage);
			this.on(Event.REMOVED,this,onRemoveFromStage);
		}

		private function onAddToStage():void
		{
			tw0 = Tween.to(img_0,{y:img_0.y + 1280,complete:Handler.create(this,this.onComplete,[img_0],false)},5000);
			tw1 = Tween.to(img_1,{y:img_1.y + 1280,complete:Handler.create(this,this.onComplete,[img_1],false)},5000);
			GameDispatcher.getInstance().on(EventName.GAME_PAUSE,this,onPause);
			GameDispatcher.getInstance().on(EventName.GAME_RESUME,this,onResume);
		}

		private function onRemoveFromStage():void
		{
			Tween.clearTween(img_0);
			Tween.clearTween(img_1);
			GameDispatcher.getInstance().off(EventName.GAME_PAUSE,this,onPause);
			GameDispatcher.getInstance().off(EventName.GAME_RESUME,this,onResume);
		}
		
		/**
		 * 循环播放
		 */
		private function onComplete(img:Image):void
		{
			if(img.y == 1280)
			{
				img.y = -1280;
			}
			if(img == img_0)
			{
				tw0 = Tween.to(img,{y:img.y + 1280,complete:Handler.create(this,this.onComplete,[img],false)},5000);
			}
			else
			{
				tw1 = Tween.to(img,{y:img.y + 1280,complete:Handler.create(this,this.onComplete,[img],false)},5000);
			}
		}

		private function onPause():void
		{
			tw0.pause();
			tw1.pause();
		}

		private function onResume():void
		{
			tw0.resume();
			tw1.resume();
		}
	}

}